package com.situ.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.pojo.entity.user.User;
import com.situ.mall.user.mapper.UserMapper;
import com.situ.mall.user.pojo.query.UserQuery;
import com.situ.mall.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CKX
 * @since 2024-10-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("name", user.getName());
        User loginUser = userMapper.selectOne(queryWrapper);
        //判断用户名是否存在
        if (loginUser == null) {
            throw new MyException("用户名不存在");
        }
        //判断密码是否正确
        if (!loginUser.getPassword().equalsIgnoreCase(user.getPassword())) {
            throw new MyException("密码错误");
        }
        return loginUser;
    }

    @Override
    public Map<String, Object> selectByPage(UserQuery userQuery) {
        IPage<User> userIPage = new Page<>(userQuery.getPage(), userQuery.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(userQuery.getName()), "name", userQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(userQuery.getPhone()), "phone", userQuery.getPhone());
        queryWrapper.between(!ObjectUtils.isEmpty(userQuery.getBeginCreateTime()) && !ObjectUtils.isEmpty(userQuery.getEndCreateTime()),
                "create_time", userQuery.getBeginCreateTime(), userQuery.getEndCreateTime());
        userMapper.selectPage(userIPage, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total", userIPage.getTotal());
        map.put("list", userIPage.getRecords());
        return map;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("status", status);
        userMapper.update(updateWrapper);
    }

    @Override
    public Boolean findUserName(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", user.getName());
        User haveUser = userMapper.selectOne(queryWrapper);
        return haveUser != null;
    }

    @Override
    public void update(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId());
        updateWrapper.set("name", user.getName()).set("phone", user.getPhone())
                .set("nick_name", user.getNickName()).set("email", user.getEmail());
        userMapper.update(updateWrapper);
    }

    @Override
    public void reCharge(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId()).set("balance", user.getBalance());
        userMapper.update(updateWrapper);
    }

    @Override
    public void updatePassword(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId()).set("password", user.getPassword());
        userMapper.update(updateWrapper);
    }

    @Override
    public Boolean findUserPassword(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", user.getId());
        queryWrapper.eq("password", user.getPassword());
        User haveUser = userMapper.selectOne(queryWrapper);
        return haveUser != null;
    }
}
